package com.ruicar.afs.cloud.channel.common.mq.sender.impl;

import cn.hutool.core.codec.Base32;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.channel.common.mq.sender.SenderChannelToArchive;
import com.ruicar.afs.cloud.channel.common.mq.sender.config.ArchiveConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @Description 归档信息发送
 * @author zyr
 * @date 2020年9月11日 17:04:10
 */
@Service
@AllArgsConstructor
public class SenderChannelToArchiveImpl {

    private SenderChannelToArchive senderChannelToArchive;

    /**
     * 归档推送
     * @param dataId 经销商ID【wwsigndealer】、用户ID【wworguser】
     * @param exchange 交换名【请从ArchiveConfig类查找对应的交换名】
     */
    public void archivePushMQ(String dataId,String exchange) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("origin",ArchiveConfig.origin);
        jsonObject.put("dataId",dataId);

        String timestamp = DateUtil.format(DateUtil.date(),DatePattern.PURE_DATETIME_MS_PATTERN);//唯一串
        String mqId = Base32.encode(timestamp, CharsetUtil.UTF_8);;

        JSONObject jsonSend= new JSONObject();
        jsonSend.put("mqId",mqId);
        jsonSend.put("exchange",exchange);
        jsonSend.put("timestamp",timestamp);
        jsonSend.put("data",jsonObject);
        jsonSend.put("remark","");

        if(ArchiveConfig.wwsigndealer.equals(exchange)){
            senderChannelToArchive.sendWwsigndealer(jsonSend.toString());
        }else if(ArchiveConfig.wworguser.equals(exchange)){
            senderChannelToArchive.sendWworguser(jsonSend.toString());
        }
    }
}
